//const mongoskin = require('mongoskin');
//https://github.com/kissjs/node-mongoskin
//var mongoose = require('mongoose'); 
//http://www.runoob.com/nodejs/nodejs-mongodb.html
const mongodb = require('mongodb');
const assert = require('assert');
export class MongoDB {

    private userName: string = "root";
    private password: string = "123456";
    private db: string = "block";
    private port: number = 27017;
    private serverUrl: string = "";
    private table: string = "";
    constructor(table: string) {
        // try {
        this.serverUrl = 'mongodb://'
            + this.userName + ':' + this.password + '@'
            + '127.0.0.1' + ':' + this.port + '/' + this.db;
        this.table = table;
        // let $this = this;
        // mongodb.MongoClient.connect(this.serverUrl, function (err, client) {
        //     assert.equal(null, err);
        //     console.log("Connected successfully to server");
        //     const db = client.db($this.db);
        //     const collection = db.collection($this.table);
        //     client.close();
        // }); 
    }



    public getConnect(callBack: Function) {
        let $this = this;
        mongodb.MongoClient.connect($this.serverUrl, function (err, client) {
            console.log("Connected successfully to server");
            const db = client.db($this.db);
            const collection = db.collection($this.table);
            callBack.call($this, collection);
            client.close();
        });
    }

    /**
     * 查询
     * @param parameter 
     */
    public select(parameter: any, mysort?: any, mylimit?: number) {
        let $this = this;
        return new Promise(function (resolve, reject) {
            $this.getConnect.call($this, (collection: any) => {
                let query = collection.find(parameter);
                if (mysort != null) {
                    query.sort(mysort);
                }
                if (mylimit != null) {
                    query.limit(mylimit);
                }
                query.toArray(function (err, docs) {
                    console.log("查询成功!");
                    if (err) {
                        reject(err);
                    }
                    else {
                        resolve(docs);
                    }
                });

            });
        });
    }

    /**
     * 查询单条
     * @param parameter 
     */
    public selectOne(parameter: any, mysort?: any) {
        let $this = this;
        return new Promise(function (resolve, reject) {
            $this.getConnect.call($this, (collection: any) => {
                collection.findOne(parameter, function (err, docs) {
                    console.log("查询成功!");
                    if (err) {
                        reject(err);
                    }
                    else {
                        resolve(docs);
                    }
                });
            });
        });
    }




    /**
     * 插入多条
     * @param parameter 
     */
    public insert(parameter: Array<any>) {
        let $this = this;
        return new Promise(function (resolve, reject) {
            $this.getConnect.call($this, (collection: any) => { 
                collection.insertMany(parameter, function (err, result) { 
                    console.log("插入成功!");
                    if (err) {
                        reject(err);
                    }
                    else {
                        resolve(result);
                    }
                });
            });
        });
    }


    /**
 * 插入单条
 * @param parameter 
 */
    public insertOne(parameter: any) {
        let $this = this;
        return new Promise(function (resolve, reject) {
            $this.getConnect.call($this, (collection: any) => {
                collection.insertOne(parameter, function (err, result) {
                    console.log("插入成功!");
                    if (err) {
                        reject(err);
                    }
                    else {
                        resolve(result);
                    }
                });
            });
        });
    }

    /**
     * 删除
     * @param parameter 
     */
    public deleteOne(parameter: any) {
        let $this = this;
        return new Promise(function (resolve, reject) {
            $this.getConnect.call($this, (collection: any) => {
                collection.deleteOne(parameter, function (err, result) {
                    console.log("删除成功!");
                    if (err) {
                        reject(err);
                    }
                    else {
                        resolve(result);
                    }
                });

            });
        });
    }


    /**
     * 删除
     * @param parameter 
     */
    public delete(parameter: any) {
        let $this = this;
        return new Promise(function (resolve, reject) {
            $this.getConnect.call($this, (collection: any) => {
                collection.deleteMany(parameter, function (err, result) {
                    console.log("删除成功!");
                    if (err) {
                        reject(err);
                    }
                    else {
                        resolve(result);
                    }
                });

            });
        });
    }


}





